/*
 * File:   Node.cpp
 * Authors: bnoleau, cybourdi, rlecrois
 *
 * Created on 18 octobre 2011, 10:58
 */

#include "Node.h"

using namespace std;

Node::Node() {
    init(1);
}

Node::Node(int nodeID) {
    init(nodeID);
}

void Node::init(int id){
	nodeID = id;
	edgeList = new list<Node*>();
}

Node::~Node() {
}

int Node::getID(){
    return nodeID;
}

list<Node*>* Node::getEdgeList(){
    return edgeList;
}

void Node::setID(int new_nodeID){
	if (new_nodeID != 0)
    		nodeID = new_nodeID;
}

int Node::outDeg(){
    return getEdgeList()->size();
}

void Node::addEdge(Node* n){
	if (*n != *this && !hasEdgeWith(n)) {
		getEdgeList()->push_back(n);
		n->getEdgeList()->push_back(this);
	}
}

bool Node::isLeaf(){
	return (outDeg() == 1);
}

bool Node::isRoot(){
	return (nodeID == 1);
}

bool  Node::hasEdgeWith(Node* n2) {
	bool hasEdge = false;

	for (list<Node*>::iterator it = getEdgeList()->begin(); it != getEdgeList()->end(); ++it)
        	if (n2->getID() == (*it)->getID())
            		return true;
	return hasEdge;
}

void Node::build(Builder *b){
	for (list<Node*>::iterator it = getEdgeList()->begin(); it != getEdgeList()->end(); ++it)
        if (getID() < (*it)->getID())
            b->addEdge(getID(), (*it)->getID());
}

bool Node::eq(const Node &n2) const {
	return nodeID == n2.nodeID;
}
bool Node::lt(const Node &n2) const {
	return nodeID < n2.nodeID;
}

bool operator<(Node const &n1, Node const &n2) {
	return n1.lt(n2);
}

bool operator==(Node const &n1, Node const &n2) {
	return n1.eq(n2);
}

bool operator!=(Node const &n1, Node const &n2) {
	return !(n1.eq(n2));
}

void Node::toString(ostream &flux){
    flux << nodeID << ": ";

    for(list<Node*>::iterator it = getEdgeList()->begin(); it!=getEdgeList()->end(); ++it)
        flux << (*it)->getID() << " ";
}

ostream &operator<<( ostream &flux, Node& node) {
    node.toString(flux) ;
    return flux;
}

